import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TankClient extends Frame {
    // 将坐标改为成员变量
    int x = 50;
    int y = 50;

    public void paint(Graphics g) {
        Color c = g.getColor();
        g.setColor(Color.red);
        g.fillOval(x, y, 30, 30);
        g.setColor(c);
        y += 5;
    }

    public void lauchFrame() {
        this.setLocation(300, 100);
        this.setSize(800, 600);
        this.setTitle("TankWar");

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        this.setResizable(false);
        this.setBackground(Color.green);
        setVisible(true);
        // 创建线程对象，传入 PaintThreak 线程类并启动
        new Thread(new PaintThreak()).start();
    }

    public static void main(String[] args) {
        TankClient tc = new TankClient();
        tc.lauchFrame();
    }

    // 创建一个内部线程类，实现 Runnable 接口
    private class PaintThreak implements Runnable {
        // 重写 run 方法
        public void run() {

            while (true) {
                // repaint 方法会自动嗲用 paint 方法
                repaint();

                try {
                    // 线程每 睡眠 50 毫秒重画一次
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}